package plsqlfs;

import java.io.File;
import java.io.FileFilter;
import java.util.regex.Pattern;

public class FileFilters {

	private FileFilters() {
		
	}

	public static FileFilter and(final FileFilter first, final FileFilter second) {
		return new FileFilter() {
			public boolean accept(File path) {
				return first.accept(path) && second.accept(path);
			}
		};
	}

	private static final FileFilter all = new FileFilter() {
		public boolean accept(File path) {
			return true;
		}
	};

	public static FileFilter all() {
		return all;
	}

	private static final FileFilter isFile = new FileFilter() {
		public boolean accept(File path) {
			return path.isFile();
		}
	};

	public static FileFilter isFile() {
		return isFile;
	}

	private static final FileFilter isDirectory = new FileFilter() {
		public boolean accept(File path) {
			return path.isDirectory();
		}
	};

	public static FileFilter isDirectory() {
		return isDirectory;
	}

	public static FileFilter forPattern(String pattern) {
		final Pattern p = Pattern.compile(pattern);
		return new FileFilter() {
			public boolean accept(File pathname) {
				return p.matcher(pathname.getName()).matches();
			}
		};
	}

}
